<?php

/**
 * GamesController - The controller for Games class
 *
 * @author
 * @version
 */

require_once 'Zend/Controller/Action.php';

class Administrator_GamesController extends Zend_Controller_Action
{

	public function init(){
            //
	}

	public function preDispatch()
	{
            $this->getHelper ('layout')->setLayout('framelayout');

            $this->view->addHelperPath(MY_ROOT.'/vhelpers');

            Zend_Controller_Action_HelperBroker::addPath(MY_ROOT.'/chelpers');

            $member=new CMember();

            if(!$member->isUser($this->_request->getModuleName(),$this->_request->getControllerName())) {
                $this->_redirect(MY_URL.'/administrator-auth-login');
                return false;
            }
            return true;
        }


    public function indexAction(){
        $page=$this->_request->getParam('page');
        $item=new CItem();

        $itemCounts=$item->getAllItemCounts('gamesfbteams');

    	Zend_Loader::loadClass("CFilter");

    	$pageArray = CFilter::filterPage ( $page, $itemCounts, 10 );
        $itemArray=$item->getAllItems($pageArray['from'],10,'game_fb_team',0);

        Zend_Loader::loadClass("CPage");

    	$pageString = CPage::divPages( $pageArray ['pageNumber'], $pageArray ['page'], ROOT_PATH."/administrator-games-index?"); //生成分页字符串

    	$this->view->assign ( "pageString", $pageString );

    	$this->view->assign ( "pageArray", $pageArray );

        $this->view->assign('itemArray',$itemArray);
    }
    
    public function teamcAction(){
        //
    }

    public function teameAction(){

	$itemid=$this->_request->getParam('itemid');

    	$item=new CItem();

    	$itemArray=$item->getDetail($itemid,'game_fb_team',false);

    	$this->view->assign('itemArray',$itemArray);

    	$attachment=new CAttachment();

    	$attachmentArray=$attachment->getAttachments($itemid);

    	$this->view->assign('attachmentArray',$attachmentArray);
        
    }

    public function doteameAction(){
        $this->_helper->viewRenderer->setNoRender();

    	if(!$this->getRequest()->isPost()){

            $this->view->showMsg("发生未知错误!");

            return false;
        }
        $paramArray=$this->_request->getParams();
        $itemid=$paramArray['itemid'];
        $filters = array(
            '*'   => 'StringTrim'
        );

        $validators = array(
        );

        $input = new Zend_Filter_Input($filters, $validators, $paramArray);

        /*修改条目表 start*/
        $item=new CItem();
        $itemData=array(
            'flid'=>0,
            'subject'=>$input->name,
            'update_time'=>time(),
        );
        $item->editItem($itemid,$itemData);
        /*修改条目表 end*/

        /*图片附件 start*/
        $resultArray=$this->editAttachment($input,$itemid);
        $attachmentData=$resultArray[0];
        $attachment=new CAttachment();
        $attachment->editAttachments($attachmentData);
        /*图片附件 end*/
        /*修改球队表 start*/
        $fbteam=new CGamefbteam();
        $data=array(
            'name'=>$input->name,
         );
        $fbteam->editFbteam($itemid,$data);
        /*修改球队表 end*/

        $this->view->showMsg("修改成功!",'administrator-games-index');

        return true;
    }

    public function doteamcAction(){
        $this->_helper->viewRenderer->setNoRender();
       
        if(!$this->getRequest()->isPost()){
             $this->view->showMsg("发生未知错误!");
             return false;
        }
        
        $thisArray=$this->_request->getParams();
        $filters = array(
        '*'   => 'StringTrim'
        );

        $validators = array(
        );

        $input = new Zend_Filter_Input($filters, $validators, $thisArray);
        /*插入到条目表 start*/
            $item=new CItem();
            $itemData=array(
                'uid'=>$_COOKIE[COOKIE_PRE.'uid'],
                'flid'=>0,
                'subject'=>$input->name,
                'item_type'=>'gamesfbteams',
                'add_time'=>time(),
                'update_time'=>time(),
                'is_banned'=>'0',
          );
        $itemid=$item->createItem($itemData);
    	/*插入到条目表 end*/

        /*图片附件 start*/
        if(count($thisArray['imgUrl'])>0){
                $resultArray=$this->addAttachment($input,$itemid);
                $attachmentData=$resultArray[0];
                $attachment=new CAttachment();
                $attachment->createAttachments($attachmentData);
        }
        /*图片附件 end*/

        /*插入到球队表 start*/
        $fbteam=new CGamefbteam();
        $data=array(
                'itemid'=>$itemid,
                'name'=>$input->name,
                'add_time'=>time(),
                'update_time'=>time(),
         );
        $fbteam->createFbteam($data);
        /*插入到球队表 end*/

        $this->view->showMsg("球队信息添加成功!",'administrator-games-index');

        return true;
    }
    public function addAttachment($input,$itemid){

    	$attachmentData=array();

		$path=new CPath();

		$image=new CImage();

		Zend_Loader::loadClass('CFile');

		for($i=0;$i<count($input->imgUrl);$i++){

			$newPath=$path->toDbPath('item',$input->imgUrl[$i]);//生成新的item图片路径

			CFile::copyAndUnlink($path->toAbsoluteRootPath($input->imgUrl[$i]),$path->toAbsoluteRootPath($newPath));//从临时文件夹转到item图片文件夹

			$cutPath=$image->cutPhoto($newPath, '200', $path->getPathFromType('cut_item'));//截图

			$attachmentData[$i]['itemid']=$itemid;

			$attachmentData[$i]['attachment_url']=$newPath;

			$attachmentData[$i]['cut_url']=$path->toDbPath('cut_item',$cutPath);

			$attachmentData[$i]['intro']=$input->imgIntro[$i];

		}

		return array($attachmentData);
    }
    
    public function editAttachment($input,$itemid){

    	$attachmentData=array();

		$path=new CPath();

		$image=new CImage();

		Zend_Loader::loadClass('CFile');

		$cut_url='';

		for($i=0;$i<count($input->imgUrl);$i++){

			$attachmentData[$i]['attachmentid']=$input->attachmentid[$i];

			if($attachmentData[$i]['attachmentid']==''){

				$newPath=$path->toDbPath('item',$input->imgUrl[$i]);//生成新的item图片路径

				CFile::copyAndUnlink($path->toAbsoluteRootPath($input->imgUrl[$i]),$path->toAbsoluteRootPath($newPath));//从临时文件夹转到item图片文件夹

				$cutPath=$image->cutPhoto($newPath, '200', $path->getPathFromType('cut_item'));//截图

				$cut_url=$path->toDbPath('cut_item',$cutPath);

				$attachmentData[$i]['attachment_url']=$newPath;

				$attachmentData[$i]['cut_url']=$cut_url;

				$attachmentData[$i]['itemid']=$itemid;

			}

			$attachmentData[$i]['intro']=$input->imgIntro[$i];



		}

		return array($attachmentData);

    }
    
    public function scenceAction(){
        
        $page=$this->_request->getParam("page",1);

        $fbscence=new CGamefbscence();

        $counts=$fbscence->getAllfbscence();

        Zend_Loader::loadClass("CFilter");

        $pageArray = CFilter::filterPage ( $page, $counts, 10 );
        $itemArray=$fbscence->getscences($pageArray['from'],10);

        Zend_Loader::loadClass("CPage");

    	$pageString = CPage::divPages( $pageArray ['pageNumber'], $pageArray ['page'], ROOT_PATH."/administrator-games-scence?"); //生成分页字符串

    	$this->view->assign ( "pageString", $pageString );

    	$this->view->assign ( "pageArray", $pageArray );

    	$this->view->assign ( 'itemArray', $itemArray );
    }

    public function scencecAction(){
        
        $fbteam=new CGamefbteam();
        
        $teamsArray=$fbteam->getteams();

        $this->view->assign ( "teamsArray", $teamsArray );
    }

    public function scenceeAction(){
        
        $id=$this->_request->getParam('id');
        
        $fbscence=new CGamefbscence();

        $editArray=$fbscence->getscencedetail($id);
        
        $this->view->assign ( "editArray", $editArray );
        
        $fbteam=new CGamefbteam();

        $teamsArray=$fbteam->getteams();

        $this->view->assign ( "teamsArray", $teamsArray );
    }

    public function dosencecAction(){
        $this->_helper->viewRenderer->setNoRender();

    	if(!$this->getRequest()->isPost()){

            $this->view->showMsg("发生未知错误!");

            return false;
        }
        $paramArray=$this->_request->getParams();
        $filters = array(
        '*'   => 'StringTrim'
        );

        $validators = array(
        );

        $input = new Zend_Filter_Input($filters, $validators, $paramArray);
        
        /*插入到比赛表 start*/
        $fbscence=new CGamefbscence();
        $data=array(
                'team_one'=>$input->team_one,
                'team_two'=>$input->team_two,
                'isend'=>$input->isend ? 1 : 0,
                'score'=>$input->score,
                'add_time'=>time(),
                'update_time'=>time(),
         );
        $fbscence->createFbscence($data);
        /*插入到比赛表 end*/

        $this->view->showMsg("球赛添加成功!",'administrator-games-scence');

        return true;
    }

    public function dosenceeAction(){
        $this->_helper->viewRenderer->setNoRender();

    	if(!$this->getRequest()->isPost()){

            $this->view->showMsg("发生未知错误!");

            return false;
        }
        $paramArray=$this->_request->getParams();
        $filters = array(
        '*'   => 'StringTrim'
        );

        $validators = array(
        );

        $input = new Zend_Filter_Input($filters, $validators, $paramArray);

        /*插入到比赛表 start*/
        $fbscence=new CGamefbscence();
        $data=array(
                'team_one'=>$input->team_one,
                'team_two'=>$input->team_two,
                'isend'=>$input->isend ? 1 : 0,
                'score'=>$input->score,
                'update_time'=>time(),
         );
        if($input->score != "" && $input->isend != ""){
            //奖励处理部分
            $score = $input->score;
            $score_s = split(":",$score);
            $winner_id = 0;
            if($score_s[0] > $score_s[1]){
                $winner_id = $input->team_one;
            }else{
                $winner_id = $input->team_two;
            }
            $bets=new CGamefbbet();
            $winBets = $bets->getAllBets(0,0,"suppout_team=$winner_id");
            if($winBets && count($winBets)>0){
                $member=new CMember();
                foreach($winBets as $k=>$v){
                    $member->updateIntegral($v['chips']*2,$v['uid'],1);
                }
            }
        }
        $fbscence->editFbscence($input->id,$data);
        /*插入到比赛表 end*/

        $this->view->showMsg("操作成功!",'administrator-games-scence');
    }

    public function betsAction(){

        $page=$this->_request->getParam('page');
        $bets=new CGamefbbet();

        $betCounts=$bets->getAllBetCounts();

    	Zend_Loader::loadClass("CFilter");

    	$pageArray = CFilter::filterPage ( $page, $betCounts, 10 );
        $betArray=$bets->getAllBets($pageArray['from'],10);

        Zend_Loader::loadClass("CPage");

    	$pageString = CPage::divPages( $pageArray ['pageNumber'], $pageArray ['page'], ROOT_PATH."/administrator-games-bets?"); //生成分页字符串

    	$this->view->assign ( "pageString", $pageString );

    	$this->view->assign ( "pageArray", $pageArray );

        $this->view->assign('betArray',$betArray);
        
    }

    public function delbetsAction(){

//
//    	$this->_helper->viewRenderer->setNoRender();
//
//    	$itemid=$this->_request->getParam('itemid');
//
//    	if($itemid==''){
//
//    		$itemidArray=$this->_request->getParam('itemArray');
//
//    	}else{
//
//    		$itemidArray=array($itemid);
//
//    	}
//        $bets=new CGamefbbet();
//        $bets->delfbbets($itemidArray);
//
//    	$this->view->showMsg('删除成功!');
    }

    public function exchangeAction(){

    }

    public function giftsAction(){

    }

    public function dodelAction(){

    	$this->_helper->viewRenderer->setNoRender();

    	$itemid=$this->_request->getParam('itemid');

    	if($itemid==''){

    		$itemidArray=$this->_request->getParam('itemArray');

    	}else{

    		$itemidArray=array($itemid);

    	}

    	$fbteam=new CGamefbteam();

    	$item=new CItem();

    	$fbteam->delFbteam($itemidArray);

    	$item->delItems($itemidArray);

    	$attachment=new CAttachment();

    	$attachment->delAttachmentsByItemid($itemidArray);

    	$this->view->showMsg('删除成功!');

    }

    public function delscenceAction(){

    	$this->_helper->viewRenderer->setNoRender();

    	$itemid=$this->_request->getParam('itemid');

    	if($itemid==''){

    		$itemidArray=$this->_request->getParam('itemArray');

    	}else{

    		$itemidArray=array($itemid);

    	}
        $fbscence=new CGamefbscence();
        $fbscence->delfbscence($itemidArray);

    	$this->view->showMsg('删除成功!');
    }
    
    //图片处理函数
    public function uploadimgAction(){

    	header("Content-type: text/html; charset=utf-8");

    	$this->getHelper ('layout')->disableLayout ();

    	$upload=new CUpload();

    	$path=new CPath();

    	$thisPath=$path->getPathFromType('temp');

    	$imageArray=$upload->uploadFile($thisPath,null,null,'500000');

    	$isSuccess=false;

    	if(is_array($imageArray)){

    		$isSuccess=true;

	    	$imgUrl=$path->toDbPath('temp',$imageArray[0]);

	    	$this->view->assign('imgUrl',$imgUrl);

    	}else{

    		$error=$imageArray;

    		$this->view->assign('error',$error);

    	}

    	$this->view->assign('isSuccess',$isSuccess);

    }
    
    public function delimgAction(){

    	$this->getHelper ('layout')->disableLayout ();

    	$this->_helper->viewRenderer->setNoRender();

    	if(unlink(MY_ROOT.'/'.$this->_request->getParam('imgurl'))){

    		echo 'success';

    	}else{

    		echo 'error';

    	}

    }

    public function delimgbyidAction(){

    	$this->_helper->viewRenderer->setNoRender();

		$this->getHelper ('layout')->disableLayout ();

    	$attachmentid=$this->_request->getParam('attachmentid');

    	$is_cut_url=$this->_request->getParam('is_cut_url');//当is_cut_url不为1即表明用户要删除的图片是截图

    	$attachment=new CAttachment();

    	$attachmentArray=$attachment->getAttachmentByAttachmentid($attachmentid);

    	$attachment_url=$attachmentArray['attachment_url'];

    	$cut_url=$attachmentArray['cut_url'];

    	$itemid=$attachmentArray['itemid'];

    	$path=new CPath();

    	@unlink($path->toAbsoluteRootPath($attachment_url));

    	@unlink($path->toAbsoluteRootPath($cut_url));

    	if($is_cut_url!=''){

			$product=new CProduct();

			$product->editProduct($itemid,array('cut_url'=>''));

    	}

    	$attachment->delAttachmentsById(array($attachmentid));

    	echo 'success';


    }
}
